// 获取应用实例
const app = getApp()

Page({

    data: {
        purchase_id: 0,
        purchase_sn: '',
        detail: null,
        table_head: [
            {name: '年份', field: 'year', is_checked: 1, width: 100},
            {name: '季节', field: 'season', is_checked: 1, width: 100},
            {name: '面料名称', field: 'material_name', is_checked: 1, width: 350},
            {name: '颜色', field: 'material_color', is_checked: 1, width: 100},
            {name: '成份', field: 'material_attr', is_checked: 1, width: 300},
            {name: '数量', field: 'confirm_num', is_checked: 1, width: 150},
            {name: '单位', field: 'unit', is_checked: 1, width: 100},
            {name: '单价', field: 'price', is_checked: 1, width: 220},
            {name: '金额', field: 'total_price', is_checked: 1, width: 220},
            {name: '货期', field: 'dead_line', is_checked: 1, width: 170},
            {name: '门幅', field: 'cloth_width', is_checked: 1, width: 150},
            {name: '克重', field: 'gram_weight', is_checked: 1, width: 110},
            // {name: '面料等级', field: 'material_level', is_checked: 1, width: 180},
            {name: '缩率', field: 'no_use_rate', is_checked: 1, width: 180},
            {name: '成品简码', field: 'template_no', is_checked: 1, width: 180},
            {name: '单件用量', field: 'single_piece', is_checked: 1, width: 180},

            {name: '面料等级', field: 'material_grade', is_checked: 1, width: 180},
            {name: '等级分类', field: 'grade_category', is_checked: 1, width: 180},
            {name: '口袋布', field: 'pocketing', is_checked: 1, width: 180},
            {name: '外发工厂', field: 'outgoing_factory', is_checked: 1, width: 180},
            {name: '加工费', field: 'template_pro_cost', is_checked: 1, width: 180},
            {name: '零售价', field: 'template_retail_price', is_checked: 1, width: 180},
            {name: '下单件数', field: 'template_order_num', is_checked: 1, width: 180},
            {name: '物料图', field: 'temp_imgs', is_checked: 1, width: 180},
            {name: '成品图', field: 'template_imgs', is_checked: 1, width: 180},
            {name: '备注', field: 'remark', is_checked: 1, width: 200},
        ],
        table_scroll_left: 0,
        purchase_data: null
    },

    onLoad(options) {
        // 设置标题
        wx.setNavigationBarTitle({
            title: '面料采购单'
        });
        // 接收参数
        if (options.purchase_id != undefined) {
            this.setData({
                purchase_id: options.purchase_id,
                purchase_sn: options.purchase_sn
            });
            // 获取数据
            this.get_data_info();
        }
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            detail: null,
            purchase_data: null,
        });
        // 获取列表
        this.get_data_info();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
        var __this = this;
        setTimeout(function () {
            __this.setData({
                table_scroll_left: e.detail.scrollLeft
            });
        }, 180);
    },

    // 获取数据
    get_data_info: function () {
        // 发起请求
        var result = app.request_api('admin/purchase/getPurchaseBomList', {
            purchase_id: this.data.purchase_id
        });
        // 处理结果
        result.then(res => {
            console.log(res);
            this.setData({
                detail: res
            });
            // 获取详情
            this.get_data_detail();
        });
    },

      // 预览图片
    PreviewImg: function (events) {
        var pic_arr = events.currentTarget.dataset.pic;
        if (pic_arr.length == 0) {
            return false;
        }
        // 调用
        wx.previewImage({
            current: pic_arr[0],
            urls: pic_arr
        })
    },
    // 获取详情
    get_data_detail: function () {
        // 发起请求
        var result = app.request_api('admin/Purchase/edit', {
            purchase_id: this.data.purchase_id
        }, true, 'GET');
        // 处理结果
        result.then(res => {
            console.log(res);
            this.setData({
                purchase_data: res.row
            });
        });
    },

    // 修改采购单
    purchase_update: function (events) {
        var __this = this;
        var purchase_id = this.data.purchase_id;
        // 跳转页面
        wx.navigateTo({
            url: '/pages/purchase_add/purchase_add?purchase_id=' + purchase_id,
            events: {
                // 当添加完成时
                changeData: function (data) {
                    __this.get_data_info();
                }
            }
        });
    },

    // 删除采购单
    purchase_delete: function (events) {
        var __this = this;
        // 确认提示
        wx.showModal({
            title: '提示',
            content: '您确定要删除吗',
            success(res) {
                if (res.confirm) {
                    __this.purchase_delete_request();
                }
            },
        });
    },

    // 删除采购单请求
    purchase_delete_request: function () {
        var __this = this;
        var purchase_id = __this.data.purchase_id;
        var result = app.request_api('admin/purchase/del', {
            ids: [purchase_id]
        }, true);
        result.then(res => {
            // 向上一级页面传递参数
            var eventChannel = __this.getOpenerEventChannel();
            eventChannel.emit('DeleteData', {

            });
            // 跳转回前一页
            wx.showModal({
                title: '提示',
                content: '删除成功',
                showCancel: false,
                complete() {
                    wx.navigateBack();
                }
            });
        });
    },

    // 审核
    purchase_audit: function (events) {
        var __this = this;
        // 确认提示
        wx.showModal({
            title: '提示',
            content: '您确定要审核通过吗',
            success(res) {
                if (res.confirm) {
                    __this.purchase_audit_request();
                }
            },
        });
    },

    // 审核请求
    purchase_audit_request: function () {
        var __this = this;
        var purchase_id = __this.data.purchase_id;
        var result = app.request_api('admin/purchase/approval', {
            ids: [purchase_id]
        }, true);
        result.then(res => {
            // 向上一级页面传递参数
            var eventChannel = __this.getOpenerEventChannel();
            eventChannel.emit('AuditData', {

            });
            __this.data.purchase_data.Posted = 1;
            __this.setData({
                purchase_data: __this.data.purchase_data
            })
            wx.showToast({
                title: '审核成功',
                icon: 'none',
                duration: 2000,
            });
        });
    },

    // 反审
    purchase_reverse: function (events) {
        var __this = this;
        // 确认提示
        wx.showModal({
            title: '提示',
            content: '您确定要反审吗',
            success(res) {
                if (res.confirm) {
                    __this.purchase_reverse_request();
                }
            },
        });
    },

    // 反审请求
    purchase_reverse_request: function (index) {
        var __this = this;
        var purchase_id = __this.data.purchase_id;
        var result = app.request_api('admin/purchase/against', {
            ids: [purchase_id]
        }, true);
        result.then(res => {
            var eventChannel = __this.getOpenerEventChannel();
            eventChannel.emit('ReverseData', {

            });
            __this.data.purchase_data.Posted = 0;
            __this.setData({
                purchase_data: __this.data.purchase_data
            })
            wx.showToast({
                title: '反审成功',
                icon: 'none',
                duration: 2000,
            });
        });
    },




})